<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use Session;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\PayModel;
class PayController extends Controller
{

    public function index()
    {
        //
        $data['data'] = PayModel::paginate(10);
        $data['type']=3 ;
        return view('pay.index',compact('data'));
    }
    public function edit(Request $request,$id)
    {
        if ($request->isMethod('get')){
            $data = PayModel::where(['id' => $id])->first()->toArray();
            return view('pay.edit', compact('data'));
        }else{
            $data = $request->except(['_token','uploadfile']);
            $validator = Validator::make($data, [
                'app_id'     => 'required|string',
                'app_secect'     => 'required|string',
                'med_id'     => 'required|string',
                'med_secect'     => 'required|string',
                'logo' =>   'required|mimes:png,jpeg,jpg',
                'price_min'     => 'required|string',
                'price_max'     => 'required|string'
            ]);
            if ($validator->fails()) {
                return redirect('/admin/pay/edit/'.$data['id'])
                    ->withErrors($validator)
                    ->withInput($data);
            }
            if(!empty($data['logo'])){
                $path = public_path('admin/logo');
                $image_path='/admin/logo/';
                $res = fileUpload($request, 'logo', $path, ['jpg', 'jpeg', 'png']);
                if($res['code']==1){
                    $data['image']=str_replace_ds($image_path.$res['msg'][1]);
                }
            }else{
                $res=PayModel::find($id);
                $data['image']=$res->logo ;
            }

            $res= PayModel::where(['id'=>$id])->update($data);

            $data['data'] = PayModel::paginate(10);
            $data['type']=3 ;
            return view('pay.index',compact('data'));
        }
    }

    public function postedit(Request $request)
    {
        $data = $request->except(['_token','uploadfile']);
        $id=$data['id'] ;
        $validator = Validator::make($data, [
            'app_id'     => 'required|string',
            'app_secect'     => 'required|string',
            'med_id'     => 'required|string',
            'med_secect'     => 'required|string',
            'price_min'     => 'required|string',
            'price_max'     => 'required|string',
        ]);
        if ($validator->fails()) {
            return redirect('/admin/pay/edit/'.$id)
                ->withErrors($validator)
                ->withInput($data);
        }
        if(!empty($data['logo'])){
            $path = public_path('admin/logo');
            $image_path='/admin/logo/';
            $res = fileUpload($request, 'logo', $path, ['jpg', 'jpeg', 'png']);
            if($res['code']==1){
                $data['logo']=str_replace_ds($image_path.$res['msg'][1]);
            }
        }else{
            $res=PayModel::find($id);
            $data['logo']=$res->logo ;
        }

        $res= PayModel::where(['id'=>$id])->update($data);

        $data['data'] = PayModel::paginate(10);
        $data['type']=3 ;
        return view('pay.index',compact('data'));
    }

}
